package com.lpf.hotelsystem.service.impl;

import com.lpf.hotelsystem.pojo.CommentEntity;
import com.lpf.hotelsystem.mapper.CommentEntityMapper;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

/**
 * 评论实体表(CommentEntity)表服务实现类
 *
 * @author makejava
 * @since 2025-01-18 17:52:21
 */
@Service("commentEntityService")
public class CommentEntityServiceImpl {
    @Resource
    private CommentEntityMapper commentEntityMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param commentId 主键
     * @return 实例对象
     */
    public Map<String, Object> selectById(Integer commentId) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", this.commentEntityMapper.selectById(commentId));
        map.put("msg", "查询单条记录成功");
        return map;
    }

    /**
     * 一次性全部查询
     *
     * @return 查询结果
     */
    public Map<String, Object> selectAll() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "全部查询成功");
        map.put("data", this.commentEntityMapper.selectAll());
        return map;
    }

    /**
     * 分页查询
     *
     * @param commentEntity 筛选条件
     * @param page        要查询第几页
     * @param limit       每页显示多少条
     * @return 查询结果
     */
    public Map<String, Object> selectByLimit(CommentEntity commentEntity, int page, int limit) {
        // page 页码   (page-1)*limit=起始下标index
        int startIndex = (page - 1) * limit;   // 计算出起始下标
        List<CommentEntity> list = this.commentEntityMapper.selectByLimit(commentEntity, startIndex, limit);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        
        // 获取满足条件的总条数
        long count = this.commentEntityMapper.selectCount(commentEntity);
        // 计算总页数
        long maxPage = (count - 1) / limit + 1;
        
        map.put("count", count);
        map.put("maxPage", maxPage);
        map.put("nowPage", page);
        map.put("data", list);
        return map;
    }
    
    /**
     * 新增数据
     *
     * @param commentEntity 实例对象
     * @return 实例对象
     */
    public Map<String, Object> insert(CommentEntity commentEntity) {
        this.commentEntityMapper.insert(commentEntity);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", commentEntity);
        map.put("msg", "新增成功");
        return map;
    }

    /**
     * 修改数据
     *
     * @param commentEntity 实例对象
     * @return 实例对象
     */
    public Map<String, Object> updateById(CommentEntity commentEntity) {
        this.commentEntityMapper.updateById(commentEntity);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", commentEntity);
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param commentId 主键
     * @return 是否成功
     */
    public Map<String, Object> deleteById(Integer commentId) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("id", commentId);
        map.put("msg", this.commentEntityMapper.deleteById(commentId) > 0 ? "删除成功" : "删除失败");
        return map;
    }
}
